package com.cs.framework.web.handler;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cs.framework.web.bind.RequestDataBinder;
import com.cs.framework.web.bo.User;
import com.cs.framework.web.context.ActionContext;
import com.cs.framework.web.util.WebConstant;
import com.cs.framework.web.view.DefaultViewName;
import com.cs.framework.web.view.ViewData;
import com.cs.framework.web.view.ViewInfo;

/**
 * Web上下文环境
 * 
 * @author hedongzhou
 * 
 */
public class Web {

	protected final static InheritableThreadLocal<ActionContext> localContext = new InheritableThreadLocal<ActionContext>() {
	};

	/**
	 * 设置上下文信息
	 * 
	 * @param context
	 */
	public static void setHandlerContext(ActionContext handlerContext) {
		localContext.set(handlerContext);
	}

	/**
	 * 清除上下文信息
	 */
	public static void remove() {
		localContext.remove();
	}

	/**
	 * 获取上下文信息
	 * 
	 * @return
	 */
	public static ActionContext getHandlerContext() {
		return localContext.get();
	}

	/**
	 * 获取请求
	 * 
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		return getHandlerContext().getRequest();
	}

	/**
	 * 获取响应
	 * 
	 * @return
	 */
	public static HttpServletResponse getResponse() {
		return getHandlerContext().getResponse();
	}

	/**
	 * 获取session
	 * 
	 * @return
	 */
	public static HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 获取请求参数
	 * 
	 * @param param
	 * @return
	 */
	public static String getParameter(String param) {
		return RequestDataBinder.getParameter(param);
	}

	/**
	 * 获取请求属性
	 * 
	 * @param key
	 * @return
	 */
	public static Object getAttribute(String key) {
		return getRequest().getAttribute(key);
	}

	/**
	 * 获取当前操作用户
	 * 
	 * @param <T>
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T extends User> T getUser() {
		return (T) getSession().getAttribute(WebConstant.CURRENT_USER);
	}

	/**
	 * 获取当前操作用户名
	 * 
	 * @return
	 */
	public static String getUserId() {
		User user = getUser();

		return user == null ? null : user.getUserId();
	}

	/**
	 * 设置当前操作用户
	 * 
	 * @param user
	 *            操作用户
	 */
	public static void setUser(User user) {
		getSession().setAttribute(WebConstant.CURRENT_USER, user);
	}

	/**
	 * 获取视图信息
	 * 
	 * @return
	 */
	public static ViewInfo getViewInfo() {
		return getHandlerContext().getViewInfo();
	}

	/**
	 * 获取视图数据
	 * 
	 * @return
	 */
	public static ViewData getViewData() {
		return getHandlerContext().getViewInfo().getViewData();
	}

	/**
	 * 设置视图名称
	 * 
	 * @param viewName
	 * @return
	 */
	public static void setViewName(String viewName) {
		getHandlerContext().getViewInfo().setViewName(viewName);
	}

	/**
	 * 添加数据
	 * 
	 * @param key
	 * @param value
	 */
	public static void addData(String key, Object value) {
		getViewData().addAttribute(key, value);
	}

	/**
	 * 添加数据
	 * 
	 * @param map
	 */
	public static void addData(Map<String, Object> map) {
		getViewData().addAttribute(map);
	}

	/**
	 * 添加数据
	 * 
	 * @param obj
	 */
	public static void addData(Object obj) {
		getViewData().addAttribute(obj);
	}

	/**
	 * 请求重定向
	 * 
	 * @param pagePath
	 */
	public static void redirect(String pagePath) {
		getViewInfo().setViewPath(pagePath);
		setViewName(DefaultViewName.REDIRECT_VIEW);
	}

	/**
	 * 请求转发
	 * 
	 * @param pagePath
	 */
	public static void forward(String pagePath) {
		getViewInfo().setViewPath(pagePath);
		setViewName(DefaultViewName.FORWARD_VIEW);
	}

	/**
	 * 根据请求，绑定对象
	 * 
	 * @param <T>
	 * @param cls
	 *            目标对象类型
	 * @return
	 */
	public static <T> T bind(Class<T> cls) {
		return RequestDataBinder.bind(cls);
	}

}
